//
//  RooterHelper.m
//  Router
//
//  Created by teki on 15/11/5.
//  Copyright © 2015年 dc. All rights reserved.
//

#import "RouterHelper.h"
#import "GCDAsyncSocket.h"

@interface RouterHelper ()
@property (nonatomic, copy) CanConnetedInternetBlock block;
@property (nonatomic, strong) GCDAsyncSocket *checkSocket;
@end


@implementation RouterHelper

- (void)canConnectedInternetBlock:(CanConnetedInternetBlock)block
{
    _block = block;
    _checkSocket = [[GCDAsyncSocket alloc] initWithDelegate:self
                                              delegateQueue:dispatch_get_main_queue()
                                                socketQueue:dispatch_get_main_queue()];
    
    [_checkSocket connectToHost:@"www.baidu.com" onPort:80 withTimeout:5 error:nil];

}



#pragma mark -
#pragma mark   AsyncSocketDelegate
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port {
    /// 能连上网
    if (sock == _checkSocket) {
        if (self.block) {
            self.block(YES);
        }
    }
    [sock disconnect];
}

- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err{
    if (err) {
        if (self.block) {
            self.block(NO);
        }
    }
}

@end
